package com.hqyj.onlineExamination.modules.exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.onlineExamination.modules.common.entity.ResultEntity;
import com.hqyj.onlineExamination.modules.common.entity.SearchBean;
import com.hqyj.onlineExamination.modules.exam.dao.QuestionDao;
import com.hqyj.onlineExamination.modules.exam.entity.Question;
import com.hqyj.onlineExamination.modules.exam.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author yun
 * @date 2021-5-25 14:25
 */
@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionDao questionDao;


    @Override
    public Question selectQuestionById(int id) {
        return questionDao.selectQuestionById(id);
    }

    @Override
    public ResultEntity<Question> addQuestionByQuestion(Question question) {
        questionDao.addQuestionByQuestion(question);
        return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.status,"添加成功！",question);
    }

    @Override
    public ResultEntity<Question> deleteQuestionById(int id) {
        questionDao.deleteQuestionById(id);
        return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.status,"删除成功！");
    }

    @Override
    public ResultEntity<Question> updateQuestionByQuestion(Question question) {
        questionDao.updateQuestionByQuestion(question);
        return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.status,"修改成功！");
    }

    @Override
    public PageInfo<Question> selectAllQuestionInPage(SearchBean searchBean) {
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getCurrentPage(),searchBean.getPageSize());
        return new PageInfo<Question>(Optional.ofNullable(questionDao.selectAllQuestionInPage(searchBean)).
                orElse(Collections.emptyList()));
    }

    @Override
    public List<Question> selectAllQuestionTypeNum() {
        return questionDao.selectAllQuestionTypeNum();
    }

    @Override
    public List<Question> getTypes() {
        int i = 1;
        List<Question> questions = questionDao.getTypes();
        for (Question question : questions) {
            question.setId(i);
            i++;
        }
        return questions;
    }

    @Override
    public List<Question> getQuestionsByType(String type) {
        return questionDao.getQuestionsByType(type);
    }

}
